﻿using BrowserCollect;
using Microsoft.Playwright;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace Common
{
    public class GoogleMapPlaceCollectScript : ICollectScript
    {
        public async Task<string> GetHtml(string url, IPage page)
        {
            await page.GotoAsync(url);
            var sw = Stopwatch.StartNew();
            while (sw.ElapsedMilliseconds <= 30_000 && !page.Url.Contains("/@"))
            {
                await Task.Delay(500);
            }
            sw.Stop();

            Sy.String pageUrl = page.Url;
            return pageUrl.Contains("/@") ? pageUrl.Mid("/@", "/").ToString() : string.Empty;
        }

        public bool IsMatch(string url) => url.StartsWith("https://www.google.com/maps/place/");
    }
}
